package br.inf.solutions.apresentacoes.ssp.control.report;

import java.awt.Color;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.component.HorizontalListBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.util.Return;
import br.com.vexillum.vexreports.control.GenericGeneratorReporter;

@Service
@Scope("prototype")
public class BaseReportGenerator extends GenericGeneratorReporter {
	
	public static final String IMAGES_PATH = "C:" + File.separator + "Applications" + File.separator + "hierarquias" + File.separator + "relatorios" + File.separator + "imagens" + File.separator;
	
	public BaseReportGenerator() {
		super();
		boldStyle = styleBuider.style().bold();
		boldCenteredStyle = styleBuider.style(boldStyle)
				.setHorizontalAlignment(HorizontalAlignment.CENTER);
		columnTitleStyle = styleBuider.style(boldCenteredStyle)
				.setBorder(styleBuider.pen1Point())
				.setBackgroundColor(Color.LIGHT_GRAY);
	}

	@Override
	protected JasperReportBuilder buildReport() {
		
		report = createReport(listReport, params, withTemplate, withHeader,
				pathTemplate, withFooter, withTitle, followAnnotation, "createHeaderReport", "createFooterReport",
				"createTitleReport", null);

		report.setColumnTitleStyle(columnTitleStyle);
		report.highlightDetailEvenRows();

		return report;
	}

	public Return createHeaderReport() {
		Return ret = new Return(true);

		JasperReportBuilder report = (JasperReportBuilder) data.get("report");
		String title = generateTitleReport();

		if (report != null) {
			if (title == null)
				title = titleReport;
			if (title != null && !title.equalsIgnoreCase("")) {
				HorizontalListBuilder horizontalList = component
						.horizontalList();
				horizontalList.add(component.text(title)
						.setStyle(boldCenteredStyle)
						.setHorizontalAlignment(HorizontalAlignment.RIGHT));
				dynamicReportsComponent = horizontalList;

				report.addPageHeader(dynamicReportsComponent);
			}
			
			List<JasperReportBuilder> listReport = new ArrayList<>();
			listReport.add(report);
			ret.setList(listReport);
		} else
			ret.setValid(false);

		return ret;
	}

	public Return createTitleReport() {
		Return ret = new Return(true);

		JasperReportBuilder report = (JasperReportBuilder) data.get("report");

		if (report != null) {
			String nameSystem = reportConfig.getKey("NAME_SYSTEM");
			String logoPath = IMAGES_PATH + "logo.png";

			HorizontalListBuilder horizontalList = component.horizontalList();
			horizontalList.add(component.image(logoPath).setFixedDimension(160, 60),
							component.verticalList(component.text(nameSystem).setStyle(boldCenteredStyle)
									.setHorizontalAlignment(HorizontalAlignment.RIGHT)));
			dynamicReportsComponent = horizontalList;

			report.addTitle(dynamicReportsComponent);
			
			List<JasperReportBuilder> listReport = new ArrayList<>();
			listReport.add(report);
			ret.setList(listReport);
		} else
			ret.setValid(false);

		return ret;
	}

	public Return createFooterReport() {
		Return ret = new Return(true);

		JasperReportBuilder report = (JasperReportBuilder) data.get("report");
		
		if(report != null){
			HorizontalListBuilder horizontalList = component.horizontalList();
			horizontalList.add(component.text(new Date()).setHorizontalAlignment(
							HorizontalAlignment.LEFT),
							component.pageXslashY().setHorizontalAlignment(HorizontalAlignment.RIGHT)
									.setStyle(styleBuider.style(boldCenteredStyle).setTopBorder(styleBuider.pen1Point())));
	
			footerComponent = horizontalList;
			report.addPageFooter(footerComponent);
			
			List<JasperReportBuilder> listReport = new ArrayList<>();
			listReport.add(report);
			ret.setList(listReport);
		} else 
			ret.setValid(false);

		return ret;
	}

}
